package com.sp2.dev.controller.blog;

import com.sp2.dev.constant.MessageProperties;
import com.sp2.dev.util.MD5Util;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: zhimingli
 * Date: 14-2-20
 * Time: 上午10:37
 * 文件上传
 */
@Controller
@RequestMapping("/blog")
public class FileuploadController {

    @RequestMapping(value = "/fileupload")
    public ModelAndView fileupload(HttpServletRequest request) {
        
        return new ModelAndView("/jsp/blog/fileupload");
    }

    @RequestMapping(value = "/upload")
    public ModelAndView upload(HttpServletRequest request,
                               @RequestParam(value = "file", required = true) MultipartFile file
    ) {
        if (request.getCharacterEncoding() == null) {
            try {
                request.setCharacterEncoding("UTF-8");//你的编码格式
            } catch (UnsupportedEncodingException e) {
            }
        }
        //确保目录存在
        String path = getPath();
        File filePath = new File(path);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }

        try {
            //保存上传的文件
            if (file != null && file.getBytes().length > 0) {
                InputStream inputStream = file.getInputStream();
                String fileName = file.getOriginalFilename();
                String newFilename = new String(fileName.getBytes("iso_8859_1"), "UTF-8");

                //如果文件存在
                File fileExist = new File(filePath + "/" + newFilename);
                if (fileExist.exists()) {//文件存在就重命名
                    int indx = fileExist.getName().indexOf(".");
                    newFilename = fileExist.getName().substring(0, indx) + System.currentTimeMillis() + fileExist.getName().substring(indx, fileExist.getName().length());
                }
                FileOutputStream fs = new FileOutputStream(filePath + "/" + newFilename);
                byte[] buffer = new byte[1024 * 1024];
                int byteread = 0;
                while ((byteread = inputStream.read(buffer)) != -1) {
                    fs.write(buffer, 0, byteread);
                    fs.flush();
                }
                fs.close();
                inputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //return "redirect:/blog/fileupload";
        return new ModelAndView(new RedirectView("/blog/fileupload"));
    }


    private String getPath() {
        return MessageProperties.save_file;
    }


    public static void main(String[] args) {
        String password = MD5Util.Md5("123");
        System.out.println(password);

        File file = new File("D:\\cmscache\\sp2\\取图片主色调.shtml");

        int indx = file.getName().indexOf(".");


        String name = file.getName().substring(0, indx) + System.currentTimeMillis() + file.getName().substring(indx, file.getName().length());
        System.out.println(name);
        System.out.println("文件名称：" + file.getName());
        System.out.println("文件是否存在：" + file.exists());
        System.out.println("文件的相对路径：" + file.getPath());
        System.out.println("文件的绝对路径：" + file.getAbsolutePath());
        System.out.println("文件可以读取：" + file.canRead());
        System.out.println("文件可以写入：" + file.canWrite());
        System.out.println("文件上级路径：" + file.getParent());
        System.out.println("文件大小：" + file.length() + "B");
        System.out.println("是否是文件类型：" + file.isFile());
        System.out.println("是否是文件夹类型：" + file.isDirectory());
    }

}
